//
//  ST_FeedBackViewController.swift
//  btvideo
//
//  Created by mac on 2020/4/21.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import SnapKit
import SwifterSwift
/**
 反馈  提意见
 */
class ST_FeedBackViewController: ST_BTLightBarViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        self.title_nav_btn.setTitleForAllStates("伴心粉丝站")
        
        view.addSubview(confirm_btn)
        confirm_btn.snp.remakeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.bottom.equalToSuperview().offset(-74+BTSafeAreaBottom-34)
            make.width.equalTo(260)
            make.height.equalTo(44)
        }
        
        
        view.addSubview(otherBtn)
        otherBtn.snp.remakeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(confirm_btn.snp.bottom)
            make.width.equalTo(260)
            make.height.equalTo(44)
        }
        
        
        
        view.addSubview(text_container_view)
        text_container_view.snp.makeConstraints { (make) in
            make.top.equalTo(own_navigation_bar.snp.bottom).offset(15)
            make.right.equalToSuperview().offset(-15)
            make.left.equalToSuperview().offset(15)
            make.height.equalTo(170)
        }
        
        view.addSubview(textView)
        textView.snp.makeConstraints { (make) in
            make.edges.equalTo(text_container_view).inset(UIEdgeInsets.init(top: 15, left: 15, bottom: 15, right: 15))
        }
        
        view.addSubview(text_place_holder_label)
        text_place_holder_label.snp.makeConstraints { (make) in
            make.top.equalTo(textView).offset(7)
            make.left.equalTo(textView).offset(5)
            make.right.equalTo(textView)
        }
        
        
        view.addSubview(contact_label)
        view.addSubview(contact_sub_label)
        
        contact_label.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(15)
            make.top.equalTo(text_container_view.snp.bottom).offset(25)
        }
        
        contact_sub_label.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(15)
            make.top.equalTo(contact_label.snp.bottom).offset(5)
        }
        
        view.addSubview(text_field_container_view)
        text_field_container_view.snp.makeConstraints { (make) in
            make.top.equalTo(contact_sub_label.snp.bottom).offset(15)
            make.right.equalToSuperview().offset(-15)
            make.left.equalToSuperview().offset(15)
            make.height.equalTo(50)
        }
        
        view.addSubview(textField)
        textField.snp.makeConstraints { (make) in
            make.edges.equalTo(text_field_container_view).inset(UIEdgeInsets.init(top: 0, left: 15, bottom: 0, right: 15))
        }
        
        text_container_view.addSubview(current_text_count_label)
        current_text_count_label.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(-10)
            make.bottom.equalToSuperview().offset(-10)
        }
        
    }
    

//     MARK: - Properties

    fileprivate lazy var confirm_btn:UIButton = {
            
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.setTitleForAllStates("提交")
        control.titleLabel?.font = UIFont.systemFont(ofSize: 15, weight: UIFont.Weight.semibold)
        control.backgroundColor = Color.init(hexString: "#FEC207")
        control.cornerRadius = 15
        control.addTarget(self, action: #selector(confirm_handle), for: UIControl.Event.touchUpInside)
        control.alpha = 0.5
        return control
    }()
    
    
    fileprivate lazy var otherBtn:UIButton = {
            
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.setTitleForAllStates("注销账号")
        control.titleLabel?.font = UIFont.systemFont(ofSize: 12, weight: UIFont.Weight.regular)
        control.setTitleColorForAllStates(UIColor.init(hex: 0xC7D2E3)!)
        control.addTarget(self, action: #selector(sign_out_handle), for: UIControl.Event.touchUpInside)
        
        return control
    }()
    
    fileprivate lazy var text_container_view:UIView = {
       let control = UIView()
        control.backgroundColor = UIColor.init(hex: 0xF5F8FA)
        control.cornerRadius = 20
        return control
    }()
    
    fileprivate lazy var textView:UITextView = {
       let control = UITextView()
        control.backgroundColor = .clear
        control.textColor = UIColor.init(hex: 0x333333)
        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.regular)
        control.tintColor = UIColor.init(hex: 0x333333)
        control.delegate = self
        return control
    }()
    
    fileprivate lazy var text_place_holder_label:UILabel = {
       let control = UILabel()
        control.numberOfLines = 0
      
        let attrString = NSMutableAttributedString(string: "感谢您成为伴心粉丝，欢迎为伴心助力。伴心正努力打造国内顶级高质量真实恋爱交友圈，我们欢迎您给出任何想法和意见，和我们一起让伴心做的更好！（请填写至少10字）")
        
        let param = NSMutableParagraphStyle()
        
        param.lineSpacing = 5
        
        let attr: [NSAttributedString.Key : Any] = [.font: UIFont.systemFont(ofSize: 14),.foregroundColor: UIColor(red: 0.67, green: 0.71, blue: 0.75, alpha: 1),.paragraphStyle:param]
        
        attrString.addAttributes(attr, range: NSRange(location: 0, length: attrString.length))
        
        control.attributedText = attrString
        
        return control
    }()
  
    
    fileprivate lazy var current_text_count_label:UILabel = {
       
        let control = UILabel()
        control.textColor = UIColor.init(hex: 0xACB4C0)
        control.font = UIFont.systemFont(ofSize: 12)
        control.text = "0/200"
        return control
    }()
    
    
    
    
    fileprivate lazy var contact_label:UILabel = {
       let control = UILabel()
        control.textColor = UIColor.init(hex: 0x333333)
        control.font = UIFont.systemFont(ofSize: 18, weight: UIFont.Weight.semibold)
        control.text = "联系方式"
        return control
    }()
    
    
    fileprivate lazy var contact_sub_label:UILabel = {
       let control = UILabel()
        control.textColor = UIColor.init(hex: 0x333333)
        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.regular)
        control.text = "愿意让我们联系你的话，请填写"
        return control
    }()
    
    fileprivate lazy var text_field_container_view:UIView = {
       let control = UIView()
        control.backgroundColor = UIColor.init(hex: 0xF5F8FA)
        control.cornerRadius = 20
        return control
    }()
    
    fileprivate lazy var textField:UITextField = {
       let control = UITextField()
        control.backgroundColor = .clear
        control.textColor = UIColor.init(hex: 0x333333)
        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.regular)
        control.tintColor = UIColor.init(hex: 0x333333)
        
        let attrString = NSMutableAttributedString(string: "请注明是微信/QQ/手机号")
        let attr: [NSAttributedString.Key : Any] = [.font: UIFont.systemFont(ofSize: 14),.foregroundColor: UIColor(red: 0.67, green: 0.71, blue: 0.75, alpha: 1)]
        attrString.addAttributes(attr, range: NSRange(location: 0, length: attrString.length))
        control.attributedPlaceholder = attrString
        
        return control
    }()
    
    fileprivate let maxCount = 200
    
    
}

extension ST_FeedBackViewController:UITextViewDelegate{
    
    
    func textViewDidChange(_ textView: UITextView) {
        text_place_holder_label.isHidden = !textView.text.isEmpty
    }
    
    func textViewDidBeginEditing(_ textView: UITextView) {
        text_place_holder_label.isHidden = !textView.text.isEmpty
    }
    
    func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
        
        guard var animal = textView.text else{
            return true
        }
        
        if let idxRange = Range.init(range, in: animal){
            
            animal.replaceSubrange(idxRange, with: text)
            
            var residueCount = maxCount - animal.count
            
            if residueCount < 0{
                residueCount = 0
            }
            
            var displayCount = animal.count
            
            if displayCount > 200{
                displayCount = 200
            }
            
            current_text_count_label.text = "\(displayCount)/200"
            
            
//            counter_label.text = "还有\(residueCount)字"
            confirm_btn.alpha =  animal.count > 9 ? 1 : 0.5
            
            return animal.count <= maxCount
            
        }else{
            return true
        }
    }
}


//MARK: - Event
extension ST_FeedBackViewController{
    
    @objc fileprivate func sign_out_handle() {
        
        self.navigationController?.pushViewController(ST_BTSignOutViewController(), animated: true)
    }
    
    @objc fileprivate func confirm_handle() {
        
        if confirm_btn.alpha < 0.9{
            return
        }
        
        var param:[String:String] = ["answer":textView.text]
        
        if let contact = textField.text{
            param.updateValue(contact, forKey: "contact")
        }
        
        self.show_indeterminate_h_u_d()
        
        JVrequest(PH.answerFeedbackApi,parameters:param, success: { (json) in
            
            ToastHud(text: "反馈成功", view: self.view)
            
            
            main_thread_execute(afterDelay: 1.7) {
                self.goback_previous_controller()
            }
            
        }) { (code, err) in
            
            ToastHud(text: err, view: self.view)
        }
        
    }
    
}
